import 'package:flutter/material.dart';

class LaraPressTabBar extends StatelessWidget {
  const LaraPressTabBar({
    super.key,
    this.tabs = const [],
    this.children = const [],
    required this.controller,
    this.isScrollable = false,
    this.tabAlignment,
    this.indicatorColor,
    this.indicatorWeight,
    this.indicatorPadding,
    this.indicatorSize,
    this.indicator,
    this.labelColor,
    this.unselectedLabelColor,
    this.labelStyle,
    this.unselectedLabelStyle,
    this.dividerColor,
    this.dividerHeight,
    this.overlayColor,
    this.splashFactory,
    this.mouseCursor,
    this.enableFeedback,
    this.onTap,
  });

  final bool isScrollable;

  final List<String> tabs;
  final List<Widget> children;
  final TabController controller;
  final TabAlignment? tabAlignment;

  // 指示器相关属性
  final Color? indicatorColor;
  final double? indicatorWeight;
  final EdgeInsetsGeometry? indicatorPadding;
  final TabBarIndicatorSize? indicatorSize;
  final Decoration? indicator;

  // 标签样式
  final Color? labelColor;
  final Color? unselectedLabelColor;
  final TextStyle? labelStyle;
  final TextStyle? unselectedLabelStyle;

  // 分割线
  final Color? dividerColor;
  final double? dividerHeight;

  // 交互效果
  final WidgetStateProperty<Color?>? overlayColor;
  final InteractiveInkFeatureFactory? splashFactory;
  final MouseCursor? mouseCursor;
  final bool? enableFeedback;
  final ValueChanged<int>? onTap;

  @override
  Widget build(BuildContext context) {
    return TabBar(
      tabs: tabs.map((e) => Tab(text: e)).toList(),
      controller: controller,
      tabAlignment: tabAlignment,
      isScrollable: isScrollable,
      indicatorColor: indicatorColor,
      indicatorWeight: indicatorWeight ?? 2.0,
      indicatorPadding: indicatorPadding ?? EdgeInsets.zero,
      indicatorSize: indicatorSize,
      indicator:
          indicator ??
          CustomTabIndicator(
            color: Theme.of(context).colorScheme.primary,
            height: 4.0,
            radius: 2.0,
            padding: EdgeInsets.symmetric(horizontal: 4.0),
          ),
      labelColor: labelColor,
      unselectedLabelColor:
          unselectedLabelColor ??
          Theme.of(context).colorScheme.onSurfaceVariant,
      labelStyle: labelStyle ?? Theme.of(context).textTheme.bodyMedium,
      unselectedLabelStyle: unselectedLabelStyle,
      dividerColor: dividerColor,
      dividerHeight: dividerHeight,
      overlayColor: overlayColor,
      splashFactory: splashFactory,
      mouseCursor: mouseCursor,
      enableFeedback: enableFeedback,
      onTap: onTap,
    );
  }
}

// 自定义指示器装饰器
class CustomTabIndicator extends Decoration {
  final Color color;
  final double height;
  final double radius;
  @override
  final EdgeInsetsGeometry padding;

  const CustomTabIndicator({
    this.color = Colors.blue,
    this.height = 3.0,
    this.radius = 2.0,
    this.padding = EdgeInsets.zero,
  });

  @override
  BoxPainter createBoxPainter([VoidCallback? onChanged]) {
    return _CustomTabIndicatorPainter(
      color: color,
      height: height,
      radius: radius,
      padding: padding,
      onChanged: onChanged,
    );
  }
}

class _CustomTabIndicatorPainter extends BoxPainter {
  final Color color;
  final double height;
  final double radius;
  final EdgeInsetsGeometry padding;

  _CustomTabIndicatorPainter({
    required this.color,
    required this.height,
    required this.radius,
    required this.padding,
    VoidCallback? onChanged,
  }) : super(onChanged);

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    final Size size = configuration.size ?? Size.zero;
    final Rect rect = offset & size;
    final Rect indicatorRect = padding
        .resolve(TextDirection.ltr)
        .deflateRect(rect);

    final Paint paint = Paint()
      ..color = color
      ..style = PaintingStyle.fill;

    final RRect rrect = RRect.fromRectAndRadius(
      Rect.fromLTWH(
        indicatorRect.left,
        indicatorRect.bottom - height,
        indicatorRect.width,
        height,
      ),
      Radius.circular(radius),
    );

    canvas.drawRRect(rrect, paint);
  }
}

// 圆形指示器
class CircularTabIndicator extends Decoration {
  final Color color;
  final double radius;
  final EdgeInsetsGeometry padding;

  const CircularTabIndicator({
    this.color = Colors.blue,
    this.radius = 4.0,
    this.padding = EdgeInsets.zero,
  });

  @override
  BoxPainter createBoxPainter([VoidCallback? onChanged]) {
    return _CircularTabIndicatorPainter(
      color: color,
      radius: radius,
      padding: padding,
      onChanged: onChanged,
    );
  }
}

class _CircularTabIndicatorPainter extends BoxPainter {
  final Color color;
  final double radius;
  final EdgeInsetsGeometry padding;

  _CircularTabIndicatorPainter({
    required this.color,
    required this.radius,
    required this.padding,
    VoidCallback? onChanged,
  }) : super(onChanged);

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    final Size size = configuration.size ?? Size.zero;
    final Rect rect = offset & size;
    final Rect indicatorRect = padding
        .resolve(TextDirection.ltr)
        .deflateRect(rect);

    final Paint paint = Paint()
      ..color = color
      ..style = PaintingStyle.fill;

    final Offset center = Offset(
      indicatorRect.center.dx,
      indicatorRect.bottom - radius,
    );

    canvas.drawCircle(center, radius, paint);
  }
}

// 渐变指示器
class GradientTabIndicator extends Decoration {
  final List<Color> colors;
  final double height;
  final double radius;
  @override
  final EdgeInsetsGeometry padding;

  const GradientTabIndicator({
    required this.colors,
    this.height = 3.0,
    this.radius = 2.0,
    this.padding = EdgeInsets.zero,
  });

  @override
  BoxPainter createBoxPainter([VoidCallback? onChanged]) {
    return _GradientTabIndicatorPainter(
      colors: colors,
      height: height,
      radius: radius,
      padding: padding,
      onChanged: onChanged,
    );
  }
}

class _GradientTabIndicatorPainter extends BoxPainter {
  final List<Color> colors;
  final double height;
  final double radius;
  final EdgeInsetsGeometry padding;

  _GradientTabIndicatorPainter({
    required this.colors,
    required this.height,
    required this.radius,
    required this.padding,
    VoidCallback? onChanged,
  }) : super(onChanged);

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    final Size size = configuration.size ?? Size.zero;
    final Rect rect = offset & size;
    final Rect indicatorRect = padding
        .resolve(TextDirection.ltr)
        .deflateRect(rect);

    final Rect paintRect = Rect.fromLTWH(
      indicatorRect.left,
      indicatorRect.bottom - height,
      indicatorRect.width,
      height,
    );

    final Paint paint = Paint()
      ..shader = LinearGradient(
        colors: colors,
        begin: Alignment.centerLeft,
        end: Alignment.centerRight,
      ).createShader(paintRect);

    final RRect rrect = RRect.fromRectAndRadius(
      paintRect,
      Radius.circular(radius),
    );
    canvas.drawRRect(rrect, paint);
  }
}
